package com.wdz.main_category.service.impl;

import com.wdz.article.entity.Article;
import com.wdz.article.mapper.ArticleMapper;
import com.wdz.category.entity.Category;
import com.wdz.category.mapper.CategoryMapper;
import com.wdz.main_category.entity.MainCategory;
import com.wdz.main_category.mapper.MainCategoryMapper;
import com.wdz.main_category.service.IMainCategoryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 主分类表 服务实现类
 * </p>
 *
 * @author 吴传逞
 * @since 2020-12-19
 */
@Service("MainCategoryServiceImpl")
public class MainCategoryServiceImpl extends ServiceImpl<MainCategoryMapper, MainCategory> implements IMainCategoryService {

    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private CategoryMapper categoryMapper;

    //根据relation_category_ids查询出relation_category_names
    private List<Article> dgRelationCategoryIds(List<Article> datas){
        Set<String> ids = new HashSet<>();
        datas.forEach(item->{
            if(item.getRelationCategoryIds() != null) {
                ids.addAll(Arrays.asList(item.getRelationCategoryIds().split(",")));
            }
        });
        if(!ids.isEmpty()) {
            List<Category> categories = categoryMapper.selectBatchIds(ids);
            datas.forEach(item1 -> {
                item1.setRelationCategorys(dgRelationCategoryIds(item1, categories));
            });

        }
        return datas;
    }


    public Article dgRelationCategoryIds(Article article){
        if(StringUtils.isEmpty(article.getRelationCategoryIds())){
            return article;
        }
        Set<String> ids = new HashSet<>();
        ids.addAll(Arrays.asList(article.getRelationCategoryIds().split(",")));
        if(!ids.isEmpty()){
            List<Category> categories = categoryMapper.selectBatchIds(ids);
            article.setRelationCategorys(categories);
        }
        return article;
    }

    private List<Category> dgRelationCategoryIds(Article article,List<Category> categories){
        if(article.getRelationCategoryIds() == null){
            return null;
        }
        String[] ids = article.getRelationCategoryIds().split(",");
        StringBuilder names = new StringBuilder();
        List<Category> categories1 = new ArrayList<>();
        for (String id : ids) {
            categories.forEach(item->{
                if(id.equals(item.getId())) {
                    Category c = new Category();
                    c.setName(item.getName());
                    c.setId(item.getId());
                    categories1.add(c);
                    c = null;
                }
            });
        }
        return categories1;
    }

    private com.wdz.resource.entity.Resource dgRelationCategoryIds(com.wdz.resource.entity.Resource resource){
        if(StringUtils.isEmpty(resource.getRelationCategoryIds())){
            return resource;
        }
        Set<String> ids = new HashSet<>();
        ids.addAll(Arrays.asList(resource.getRelationCategoryIds().split(",")));
        if(!ids.isEmpty()){
            List<Category> categories = categoryMapper.selectBatchIds(ids);
            resource.setRelationCategorys(categories);
        }
        return resource;
    }

    //根据relation_category_ids查询出relation_category_names
    private List<com.wdz.resource.entity.Resource> dgRelationCategoryIds2(List<com.wdz.resource.entity.Resource> datas){
        Set<String> ids = new HashSet<>();
        datas.forEach(item->{
            if(item.getRelationCategoryIds() != null) {
                ids.addAll(Arrays.asList(item.getRelationCategoryIds().split(",")));
            }
        });
        if(!ids.isEmpty()) {
            List<Category> categories = categoryMapper.selectBatchIds(ids);
            datas.forEach(item1 -> {
                item1.setRelationCategorys(dgRelationCategoryIds(item1, categories));
            });

        }
        return datas;
    }
    private List<Category> dgRelationCategoryIds(com.wdz.resource.entity.Resource resource, List<Category> categories){
        if(resource.getRelationCategoryIds() == null){
            return null;
        }
        String[] ids = resource.getRelationCategoryIds().split(",");
        StringBuilder names = new StringBuilder();
        List<Category> categories1 = new ArrayList<>();
        for (String id : ids) {
            categories.forEach(item->{
                if(id.equals(item.getId())) {
                    Category c = new Category();
                    c.setName(item.getName());
                    c.setId(item.getId());
                    categories1.add(c);
                    c = null;
                }
            });
        }
        return categories1;
    }

    @Resource
    private MainCategoryMapper mainCategoryMapper;
    @Override
    public List<Article> articles(String id, Integer pageNo, Integer pageSize) {
        return dgRelationCategoryIds(mainCategoryMapper.articles(id, (pageNo-1)*pageSize,pageSize));
    }

    @Override
    public Integer articlesCount(String id) {
        return mainCategoryMapper.articlesCount(id);
    }

    @Override
    public List<Article> articlesTopping(String id) {
        return dgRelationCategoryIds(mainCategoryMapper.articlesTopping(id));
    }

    @Override
    public List<com.wdz.resource.entity.Resource> resources(String id, Integer pageNo, Integer pageSize) {
        return dgRelationCategoryIds2(mainCategoryMapper.resources(id, (pageNo-1)*pageSize,pageSize));
    }

    @Override
    public Integer resourcesCount(String id) {
        return mainCategoryMapper.recourcesCount(id);
    }

}
